﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors;
using COMOSOFT.Class;

namespace COMOSOFT.MatarialManagement.Forms
{
    public partial class FrmBookMA : Form
    {
        ClassShare cls = new ClassShare("FrmBookMA");
        int BMID, BDID;  
        string MANo, UnitsPrice, Remark;
        double Price;
        int MAID, MADID, Qty;
        string MALDIDs;
        public FrmBookMA(int _BMID)
        {
            InitializeComponent();
            this.BMID=_BMID;
        }
        
        private void FrmBookMA_Load(object sender, EventArgs e)
        {            
            this.employeeTableAdapter.Fill(this.dataSetOrderManagement.Employee);
            this.mAListDetailsTableAdapter.Fill(this.dataSetBalancing.MAListDetails);
            this.sp_BookChooseMATableAdapter.Fill(this.dataSetBalancing.sp_BookChooseMA);         
            this.sp_BookChooseTrimListTableAdapter.Fill(this.dataSetBalancing.sp_BookChooseTrimList);
            this.supplierTableAdapter.Fill(dataSetMatarial.Supplier);     
        }

        public void AddNew()
        {
            bookingMasterBindingSource.AddNew();
            txt_IssueDate.EditValue = DateTime.Now;
        }

        public void UpdateData()
        {
            try
            {
                if (BMID < 0)
                {
                    bookingMasterBindingSource.EndEdit();
                    bookingMasterTableAdapter.Update(dataSetBalancing.BookingMaster);
                    dataSetBalancing.BookingMaster.AcceptChanges();
                    group_BookDetais.Enabled = true;
                    BMID = Convert.ToInt32(txt_BID.Text);
                }
                else
                {
                    bookingMasterBindingSource.EndEdit();
                    bookingMasterTableAdapter.Update(dataSetBalancing.BookingMaster);
                    dataSetBalancing.BookingMaster.AcceptChanges();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void DeleteBMA()
        {
            try
            {
                if (MessageBox.Show("Are you sure you want to delete?", "Delete item", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    dataSetBalancing.BookingMaster[0].Delete();
                    bookingMasterTableAdapter.Update(dataSetBalancing.BookingMaster);
                    dataSetBalancing.BookingMaster.AcceptChanges();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void loadData()
        {
            try
            {
                if (BMID > 0)
                {
                    this.bookingMasterTableAdapter.Fill(this.dataSetBalancing.BookingMaster, BMID);
                    this.sp_BookDetailsTableAdapter.Fill(this.dataSetBalancing.sp_BookDetails,BMID);
                    group_BookDetais.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dataNavigator_BookMA_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            try
            {
                if (e.Button.Tag.ToString() == "Save")
                {

                    if (BMID < 0)
                    {
                        bookingMasterBindingSource.EndEdit();
                        bookingMasterTableAdapter.Update(dataSetBalancing.BookingMaster);
                        dataSetBalancing.BookingMaster.AcceptChanges();                        
                        BMID = Convert.ToInt32(txt_BID.Text);
                    }

                    //Update Details
                    DataTable dtb = cls.GetData("sp_BookDetailsInsert", new string[] { "@BMID", "@MAID", "@MADID", "@Qty" }, new object[] { BMID, MAID, MADID, Qty });
                    if (MALDIDs != null)
                    {
                        string[] arr = MALDIDs.Split(new string[] { "," }, StringSplitOptions.None);
                        for (int i = 0; i < arr.Count(); i++)
                        {
                            cls.UpdateData("sp_BookAllocateInsert", new string[] { "@BDID", "@MALDID" }, new object[] { Convert.ToInt32(dtb.Rows[0][0]), Convert.ToInt32(arr[i]) });
                        }
                    }
                    //---
                    group_BookDetais.Enabled = true;
                    this.Text = txt_BookNo.Text;
                    loadData();
                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gridView_BookDetails_DoubleClick(object sender, EventArgs e)
        {
            if (gridView_BookDetails.FocusedRowHandle >= 0)
            {
                sp_BookAllocateTableAdapter.Fill(dataSetBalancing.sp_BookAllocate, Convert.ToInt32(gridView_BookDetails.GetFocusedDataRow()["BDID"]));
                this.supplierTableAdapter.Fill(this.dataSetMatarial.Supplier);                
            }
        }
        //--------------
        private void gridView_BookDetails_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            try
            {
                GridView view = sender as GridView;
                if (view.FocusedRowHandle >= 0 && !view.IsGroupRow(e.RowHandle))
                {
                    MANo = view.GetFocusedDataRow()["MANo"].ToString();
                    if (view.GetFocusedDataRow()["BDID"].ToString() != "")
                        BDID = Convert.ToInt32(view.GetFocusedDataRow()["BDID"]);
                    if (view.GetFocusedDataRow()["Price"].ToString() != "")
                        Price = Convert.ToDouble(view.GetFocusedDataRow()["Price"]);
                    UnitsPrice = view.GetFocusedDataRow()["UnitsPrice"].ToString();
                    if (view.GetFocusedDataRow()["BQty"].ToString() != "")
                        Qty = Convert.ToInt32(view.GetFocusedDataRow()["BQty"]);
                    Remark = view.GetFocusedDataRow()["Remarks"].ToString();
                    cls.UpdateData("sp_BookUpdate", new string[] { "@BDID", "@MANo", "@Price", "@UnitsPrice", "@BQty", "@Remarks" }, new object[] { BDID, MANo, Price, UnitsPrice, Qty, Remark });
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gridControl_BookDetails_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (gridView_BookDetails.FocusedRowHandle >= 0)
                {
                    sp_BookAllocateTableAdapter.Fill(dataSetBalancing.sp_BookAllocate, Convert.ToInt32(gridView_BookDetails.GetFocusedDataRow()["BDID"]));
                    this.supplierTableAdapter.Fill(this.dataSetMatarial.Supplier);                    
                }
            }
        }

        private void bnt_BCancel_Click(object sender, EventArgs e)
        {
            PopupContainerEdit pp = gridView_BookDetails.ActiveEditor as PopupContainerEdit;
            pp.ClosePopup();
        }

        private void bnt_BOK_Click(object sender, EventArgs e)
        {
            try
            {
                GridView view = gridView_BookMA as GridView;
                GridView cView= view.GetDetailView(view.FocusedRowHandle, 0) as GridView;
                if (view.FocusedRowHandle >= 0 && !view.IsGroupRow(view.FocusedRowHandle))
                {
                    int MAID = Convert.ToInt32(view.GetFocusedDataRow()["MAID"]);
                    int MADID = Convert.ToInt32(view.GetFocusedDataRow()["MADID"]);
                    if (view.GetFocusedDataRow()["BookQty"].ToString() != "")
                        Qty = Convert.ToInt16(view.GetFocusedDataRow()["BookQty"]);
                    DataTable dtb= cls.GetData("sp_BookDetailsInsert", new string[] { "@BMID", "@MAID", "@MADID", "@Qty" }, new object[] { BMID, MAID, MADID, Qty });
                    if (cView != null)
                    {
                        int[] rows = cView.GetSelectedRows();
                        foreach (int i in rows)
                        {
                            cls.UpdateData("sp_BookAllocateInsert", new string[] { "@BDID", "@MALDID" }, new object[] { Convert.ToInt32(dtb.Rows[0][0]), Convert.ToInt32(cView.GetDataRow(i)["MALDID"]) });
                        }
                        cView.ClearSelection();
                    }
                    loadData();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gridView_BookDetails_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            if (e.Column.FieldName == "Art")
            {
                this.sp_BookChooseMATableAdapter.Fill(this.dataSetBalancing.sp_BookChooseMA);
                this.sp_BookChooseTrimListTableAdapter.Fill(this.dataSetBalancing.sp_BookChooseTrimList);
            }
        }

        public void ShowControl()
        {
            dataNavigator_BookMA.Visible = true;
            panelControl_Finished.Visible = true;
        }

        private void gridView_BookMA_MasterRowExpanded(object sender, CustomMasterRowEventArgs e)
        {
            GridView view = sender as GridView;
            GridView cView= view.GetDetailView(view.FocusedRowHandle, 0) as GridView;
            cView.ClearSelection();
        }

        private void bnt_OK_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void SetValue(int MAID,int MADID, int Qty,string MALDIDs)
        {           
            this.MAID = MAID;
            this.MADID = MADID;
            this.Qty = Qty;
            this.MALDIDs = MALDIDs;
        }
    }
}
